package com.znsd.zl.sys.audit.controller;

import com.znsd.zl.sys.audit.entity.AuditEntity;
import com.znsd.zl.sys.audit.service.IAuditService;
import com.znsd.zl.sys.sys.entity.SysUserEntity;
import com.znsd.zl.utils.BaseController;
import com.znsd.zl.utils.DateUtils;
import com.znsd.zl.utils.PageUtils;
import com.znsd.zl.utils.R;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 *
 *
 * @author jbb
 * @date 2020-08-17 11:38:51
 */
@RestController
@RequestMapping("sys/audit")
public class AuditController extends BaseController<IAuditService,AuditEntity,Integer> {

    /**
     * 列表
     */
    @GetMapping("/{page}/{limit}")
    @Override
    public R pageFind(@RequestParam Map<String, Object> params,@PathVariable("page")Integer page,@PathVariable("limit")Integer limit){
        System.out.println(params);
        PageUtils pu = service.queryPage(params,page,limit);

        return R.ok().put("page", pu);
    }

    /**
     * 选择性修改
     */
    @PutMapping("/{id}")
    public R selectUpdate(@RequestBody Map<String, Object> params,@PathVariable("id") Integer id) {
        try {
            params.put("update_time",DateUtils.format(new Date()    ,DateUtils.DATE_TIME_PATTERN));
            Object app = params.remove("app");
            if (app == null) {
                params.put("operator",((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername());
            }
            service.selectUpdate(params,id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error("服务器异常，请稍后重试");
        }
    }

    /**
     * 添加房屋图片
     */
    @PostMapping("saveHouseCover")
    public R saveHouseCover(@RequestBody Map<String,Object> params){
        String houseId = String.valueOf(params.get("houseId"));
        String createTime = String.valueOf(params.get("createTime"));
        String operator = String.valueOf(params.get("operator"));
        List list = (List) params.get("list");

        System.out.println(houseId+"=="+list+"=="+createTime+"=="+operator);
        service.saveHouseCover(houseId,list,createTime,operator);
        return R.ok();
    }

}
